﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 链接数据库
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection sqlConnection = new SqlConnection("Data Source=.;Initial Catalog=schoolDB;Integrated Security=True");
                string strSQL = "select Sno,Sname from Student";
                SqlCommand sqlCommand = new SqlCommand(strSQL, sqlConnection);
                sqlConnection.Open();
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                string strMsg = "";
                while (sqlDataReader.Read())
                {
                    string strID = (string)sqlDataReader["Sno"];
                    string strName = (string)sqlDataReader["Sname"];
                    strMsg = strMsg + "Sno:" + strID + "\tSname:" + strName + "\n";
                }
                sqlConnection.Close();
                MessageBox.Show(strMsg);
            }
            catch (Exception m)
            {
                MessageBox.Show(m.ToString());
            }
        }

        }
    }

